package com.yanqu.road.server.manger.activity.shuihu;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.shuihu.ShuiHuUserData;
import com.yanqu.road.entity.activity.shuihu.config.ShuiHuCombConfig;
import com.yanqu.road.entity.activity.shuihu.config.ShuiHuConfig;
import com.yanqu.road.entity.enums.eActivityConfigType;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.bussiness.activity.ShuiHuActivityBussiness;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.shuihu.ShuiHuModule;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ShuiHuMgr extends TempMgr {

    private static ShuiHuConfig shuiHuConfig;

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

    @Override
    public boolean save() {
        return true;
    }

    public static void reloadActivityData() {
        getLogger().info("reload ShuiHu Activity start");

        // 活动信息 activityInfo
        List<ActivityInfo> openActivityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.ShuiHuActivity.getValue());
        if (openActivityInfoList.size() == 0) {
            getLogger().info("no ShuiHu Activity in show time");
            return;
        }
        ActivityInfo activityInfo = openActivityInfoList.get(0);

        // 获取配置
        shuiHuConfig = loadShuiHuConfigFromDb(activityInfo);

        getLogger().info("reload ShuiHu activity finish");

        reloadPlayerData();
    }

    /**
     * 数据库读配置
     */
    public static ShuiHuConfig loadShuiHuConfigFromDb(ActivityInfo activityInfo) {
        int activityId = activityInfo.getActivityId();
        List<Integer> activityIdList = new ArrayList<>();
        activityIdList.add(activityId);

        ShuiHuConfig tmpConfig = new ShuiHuConfig();
        tmpConfig.setActivityId(activityId);
        tmpConfig.setActivityInfo(activityInfo);
        // 卡片
        tmpConfig.setCardConfigMap(ShuiHuActivityBussiness.getShuiHuCardConfigMap(activityInfo.getActivityId()));
        // 组合
        tmpConfig.setCombConfigMap(ShuiHuActivityBussiness.getShuiHuCombConfigMap(activityInfo.getActivityId()));
        // 其他配置
        Map<String, ActivityConfig> activityConfigMap = ActivityBussiness.getActivityConfigMap(activityIdList).get(activityId);
        tmpConfig.setDrawCost(activityConfigMap.get(eActivityConfigType.SHUIHU_DRAW_COST.getName()).getValue());
        tmpConfig.setNoticeMaxCount(activityConfigMap.get(eActivityConfigType.SHUIHU_NOTICE_MAX_COUNT.getName()).getIntValue());
        tmpConfig.setQualityCountList(activityConfigMap.get(eActivityConfigType.SHUIHU_QUALITY_COUNT.getName()).getIntListValue("\\|"));
        tmpConfig.setBigRewardCardId(activityConfigMap.get(eActivityConfigType.SHUIHU_BIG_REWARD_CARD_ID.getName()).getIntValue());
        tmpConfig.setSpecialQualityList(activityConfigMap.get(eActivityConfigType.SHUIHU_SPECIAL_QUALITY.getName()).getValue());

        return tmpConfig;
    }

    public static void reloadPlayerData() {
        for (GamePlayer player : GamePlayerMgr.getCopyPlayerMap().values()) {
            player.getModule(ShuiHuModule.class).syncShuiHuConfig();
            player.getModule(ShuiHuModule.class).initUserData();
            player.getModule(ShuiHuModule.class).syncShuiHuUserData();
        }
    }

    public static ActivityInfo getActivityInfo() {
        return shuiHuConfig == null ? null : shuiHuConfig.getActivityInfo();
    }

    public static ShuiHuConfig getShuiHuConfig() {
        return shuiHuConfig;
    }

    /**
     * 活动期或领奖期
     */
    public static boolean activityInTimeOrRewardTime() {
        if (null == shuiHuConfig) {
            return false;
        }
        long now = System.currentTimeMillis() / 1000;
        ActivityInfo activityInfo = shuiHuConfig.getActivityInfo();
        return activityInfo.getBeginTime() <= now && activityInfo.getEndShowTime() > now;
    }

    /**
     * 活动结束发奖
     *
     * @return 奖励
     */
    public static Property autoSendReward(ShuiHuConfig config, ShuiHuUserData userData) {
        Property reward = new Property();

        Property cardProp = userData.getCardProp();
        Property combProp = userData.getCombProp();

        for (ShuiHuCombConfig combConfig : config.getCombConfigMap().values()) {
            // 先等于剩余次数
            int maxTimes = combConfig.getMaxTimes() - combProp.getCountByGoodsId(combConfig.getId()).intValue();
            Property condition = PropertyHelper.parseStringToProperty(combConfig.getCondition());
            for (Map.Entry<Integer, BigInteger> entry : condition.getGoods().entrySet()) {
                if (maxTimes == 0) {
                    break;
                }
                int tmpTimes = cardProp.getCountByGoodsId(entry.getKey()).intValue() / entry.getValue().intValue();
                // 取最小次数
                maxTimes = maxTimes < tmpTimes ? maxTimes : tmpTimes;
            }

            if (maxTimes <= 0) {
                // 该组合不满足条件
                continue;
            }

            // 添加奖励
            for (Map.Entry<Integer, BigInteger> entry : condition.getGoods().entrySet()) {
                cardProp.removeProperty(entry.getKey(), entry.getValue().multiply(BigInteger.valueOf(maxTimes)));
            }
            Property tmpReward = PropertyHelper.parseStringToProperty(combConfig.getReward());
            tmpReward.rideProperty(maxTimes);
            reward.addProperty(tmpReward);

            // 组合加次数
            combProp.addProperty(combConfig.getId(), BigInteger.valueOf(maxTimes));
        }

        return reward;
    }

    /**
     * 计算所有组合达成的次数
     */
    public static int calAllCombTimes(ShuiHuConfig shuiHuConfig, ShuiHuUserData userData) {
        int minTimes = Integer.MAX_VALUE;
        Property combProp = userData.getCombProp();

        for (ShuiHuCombConfig combConfig : shuiHuConfig.getCombConfigMap().values()) {
            int times = combProp.getCountByGoodsId(combConfig.getId()).intValue();
            if (times < minTimes) {
                minTimes = times;
            }
            if (minTimes == 0) {
                // 没有比0更小了
                break;
            }
        }

        if (minTimes == Integer.MAX_VALUE) {
            minTimes = 0;
        }

        return minTimes;
    }
}
